package cn.wolfcode.wms.controllrer;

import cn.wolfcode.wms.qo.OrderChartQueryObject;
import cn.wolfcode.wms.qo.SaleChartQueryObject;
import cn.wolfcode.wms.service.IBrandService;
import cn.wolfcode.wms.service.IChartsService;
import cn.wolfcode.wms.service.ICilentService;
import cn.wolfcode.wms.service.ISupplierService;
import cn.wolfcode.wms.util.RequiredPermission;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chart")
public class ChartsController {

    @Autowired
    private IChartsService chartsService;

    @Autowired
    private ISupplierService supplierService;

    @Autowired
    private IBrandService brandService;

    @Autowired
    private ICilentService cilentService;

    @RequestMapping("order")
    @RequiredPermission("订单报表")
    public String orderChart(@ModelAttribute("qo") OrderChartQueryObject qo, Model model){
        model.addAttribute("groupTypes",OrderChartQueryObject.groupBys );
        model.addAttribute("suppliers",supplierService.selectAll());
        model.addAttribute("brands",brandService.selectAll());
       model.addAttribute("datas",chartsService.orderChart(qo));

        return "charts/order";


    }

    @RequestMapping("sale")
    @RequiredPermission("销售报表")
    public String saleChart(@ModelAttribute("qo") SaleChartQueryObject qo, Model model){
        model.addAttribute("groupTypes",SaleChartQueryObject.groupBys );
        model.addAttribute("clients",supplierService.selectAll());
        model.addAttribute("brands",cilentService.selectAll());
        model.addAttribute("datas",chartsService.saleChart(qo));

        return "charts/sale";
    }


    @RequestMapping("/saleCharByBar")
    @RequiredPermission("/销售报表-柱状图")
    public  String saleCharByBar(@ModelAttribute("qo") SaleChartQueryObject qo,Model model){
        List<Map<String, Object>> maps = chartsService.saleChart(qo);
        List<Object> groupTypes = new ArrayList<>();
        List<Object> totalAmounts = new ArrayList<>();
        //封装横坐标数据 , (分组类型)以及纵坐标数据(销售总额)
        for (Map<String, Object> objectMap : maps) {
            groupTypes.add(objectMap.get("groupType"));
            totalAmounts.add(objectMap.get("totalAmount"));
        }
        //特殊元素{字符串,对象,数组等}需要转换为JSON格式
        model.addAttribute("groupTypes", JSON.toJSONString(groupTypes));
        model.addAttribute("totalAmounts",totalAmounts);
        model.addAttribute("groupName",SaleChartQueryObject.groupBys.get(qo.getGroupBy()));
        return "charts/saleCharByBar";
    }


    @RequestMapping("/saleCharByPie")
    @RequiredPermission("/销售报表-饼状图")
    public  String saleCharByPie(@ModelAttribute("qo") SaleChartQueryObject qo,Model model){
        List<Map<String, Object>> maps = chartsService.saleChart(qo);
        model.addAttribute("groupName",SaleChartQueryObject.groupBys.get(qo.getGroupBy()));
        //图表横坐标
        List<Object> groupTypes = new ArrayList<>();
        //图表比例数据
        List<Object> datas = new ArrayList<>();
        BigDecimal maxAmount = BigDecimal.ZERO;
        for (Map<String, Object> objectMap  : maps) {
            //获取最大值
            BigDecimal amount = (BigDecimal) objectMap.get("totalAmount");
            //比较最大值,然后交换
            if(amount.compareTo(maxAmount) > 0){
                maxAmount = amount;

            }

            //封装横坐标数据
            groupTypes.add(objectMap.get("groupType"));
            //封装饼总的单个数据
            Map<Object, Object> data = new HashMap<>();
            data.put("name",objectMap.get("groupType"));
            data.put("value",objectMap.get("totalAmount"));
            datas.add(data);
        }
        model.addAttribute("groupTypes", JSON.toJSONString(groupTypes));
        model.addAttribute("datas",JSON.toJSONString(datas));
        model.addAttribute("maxAmount",maxAmount);
        return "charts/saleCharByPie";
    }



}
